package com.chaochis.springboottestdemo.controller;

import com.chaochis.springboottestdemo.base.ApiResult;
import com.chaochis.springboottestdemo.config.reqResConfig.dispatcherConfig.CustomRequestWrapper;
import com.chaochis.springboottestdemo.model.apiResultVo.ApiAuthUser;
import com.chaochis.springboottestdemo.model.apiResultVo.ApiUser;
import com.chaochis.springboottestdemo.model.mapperModel.UserVo;
import com.chaochis.springboottestdemo.model.queryParam.LoginParam;
import com.chaochis.springboottestdemo.service.UserLoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController("login")
@RequestMapping("/user")
public class LoginController {

  private final UserLoginService userLoginService;

  public LoginController(UserLoginService userLoginService) {
    this.userLoginService = userLoginService;
  }

  @PostMapping("/login")
  public ApiResult loginSystem(@ApiIgnore CustomRequestWrapper customRequestWrapper, @RequestBody LoginParam loginParam) {
    var apiAuthUser = new ApiAuthUser();
    try {
      var loginUser = this.userLoginService.login(customRequestWrapper, loginParam);
      var apiUser = new ApiUser();
      BeanUtils.copyProperties(loginUser.getUserVo(), apiUser);
      apiAuthUser.setToken(loginUser.getToken());
      apiAuthUser.setApiUser(apiUser);
    } catch (Exception e) {
      return ApiResult.fail(e.getMessage());
    }
    return ApiResult.ok(apiAuthUser);
  }

  @PostMapping("/register")
  public ApiResult register(@RequestBody UserVo userVo) {
    return ApiResult.ok();
  }
}
